{extend name='table'}

{block name="content"}
<div class="layui-tab layui-tab-card">
    <div class="layui-tab-content">
        {include file='order/order_express/index_search'}
        <iframe id="printframe" style="display:none;"></iframe>
        <table id="order" data-url="{:sysuri()}" data-target-search="form.form-search"></table>
    </div>
</div>
{/block}

{block name='script'}
<script>
    $(function () {
        // 初始化表格组件
        $('#order').layTable({
            even: true, height: 'full',
            // sort: {field: 'id', type: 'desc'},
            toolbar: '#toolbarDemo',
            cols: [[
                {checkbox: true, fixed: true},
                /** {if $super} **/
                {field: 'company_name', title: '{:lang("公司")}', minWidth: 130, align: 'center'},
                /** {/if} **/
                {field: 'warehouse_c_name', title: '{:lang("仓库名称")}', width: 130, align: 'center'},
                {field: 'order_no', title: '{:lang("第三方订单号")}', minWidth: 140, align: 'center'},
                {field: 'express_company', title: '{:lang("物流名称")}', minWidth: 110, align: 'center'},
                {field: 'express_no', title: '{:lang("物流单号")}', minWidth: 140, align: 'center'},
                {field: 'location_code', title: '{:lang("货位")}', width: 80, align: 'center'},
                {field: 'status_title', title: '{:lang("状态")}', width: 70, align: 'center'},
                {field: 'is_pid', title: '{:lang("合箱状态")}', width: 70, align: 'center'},
                {field: 'confirm_time', title: '{:lang("推送时间")}', width: 150, align: 'center'},
                {field: 'into_time', title: '{:lang("入库时间")}', width: 150, align: 'center'},
                {field: 'create_at', title: '{:lang("添加时间")}', width: 150, align: 'center'},
                {toolbar: '#toolbar', align: 'center', width: 122, title: '{:lang("操作")}', fixed: 'right'},
            ]]
        });

        // 工具栏事件
        layui.table.on('toolbar(order)', function(obj){
            let id = obj.config.id
            let checkStatus = layui.table.checkStatus(id)
            let data = checkStatus.data
            // var othis = lay(this)

            let ids = []
            let sheet_urls = []
            for (let i = 0; i < data.length; i++){
                ids[i] = data[i].id
                sheet_urls[i] = data[i].sheet_url
            }

            switch(obj.event){
                case 'print_express': // 打印快递面单 todo 请求后端合并pdf

                    // 删除所有的iframe标签
                    const iframes = document.querySelectorAll('iframe');
                    iframes.forEach(function(iframe) {
                        iframe.remove();
                    });

                    // sheet_urls = ["","/upload/20241206/1.pdf","2","/upload/20241206/2.pdf",""];
                    // sheet_urls = ["/upload/20241206/1.pdf","/upload/20241206/2.pdf"];
                    // sheet_urls = ["/upload/20241206/1.pdf"];

                    // 去重
                    // sheet_urls = Array.from(new Set(sheet_urls))
                    // console.log(sheet_urls)

                    sheet_urls.forEach(function(sheet_url) {

                        if(!sheet_url){
                            return true; // 跳出当前循环
                        }

                        console.log(sheet_url)
                        let iframe = document.createElement('iframe');
                        iframe.style.display = 'none';

                        // 将 iframe 添加到页面中
                        document.body.appendChild(iframe);
                        // 设置 iframe 的 src 为当前 PDF URL
                        iframe.src = sheet_url;

                        // 在 iframe 加载完成后打印
                        iframe.onload = function() {
                            try {

                                // console.log("iframe.onload");
                                // 确保打印执行
                                iframe.contentWindow.print();

                                // setTimeout(function() {
                                    // 移除 iframe
                                    // document.body.removeChild(iframe);
                                // }, 1000);

                            } catch (e) {
                                console.error("无法调用打印：", e);
                            }
                        };
                    });

                    break;
            };
        });

    });
</script>

<script type="text/html" id="toolbarDemo">
    <!--{if auth("infoSignFor")}-->
    <button class="layui-btn layui-btn-xs layui-btn-normal" data-rule="id#{id}" data-modal="{:url('infoSignFor')}" data-table-id="order">签收</button>
    <!--{/if}-->
    <a lay-event="print_express" class='layui-btn layui-btn-xs layui-btn-primary'>{:lang('打印快面单')}</a>
</script>

<!-- 数据操作工具条模板 -->
<script type="text/html" id="toolbar">
    <!--{if auth('info')}-->
    <a class="layui-btn layui-btn-xs layui-btn-primary" data-title="{:lang('查看')}" data-modal='{:url("info")}?id={{d.id}}'>{:lang('查看')}</a>
    <!--{/if}-->
    <!--{if auth('infoWarehouseIn')}-->
    {{# if(d.status == 1){ }}
    <a class="layui-btn layui-btn-xs" data-title="{:lang('入库')}" data-modal='{:url("infoWarehouseIn")}?id={{d.id}}'>{:lang('入库')}</a>
    {{# } }}
    <!--{/if}-->

    <!--{if auth("remove")}-->
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" data-confirm="{:lang('确定要删除数据吗?')}" data-action="{:url('remove')}" data-value="id#{{d.id}}">{:lang('删 除')}</a>-->
    <!--{/if}-->
</script>
{/block}